package Controllers;

import Bean.BeanTeacher;
import Bean.BeanUser;
import Impl.TeacherServiceImpl;
import Impl.UserServiceImpl;
import com.alibaba.fastjson.JSONArray;
import exception.BooksNotFoundException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Controller
public class ShowAllTeacherController extends HttpServlet {

    private static final long serialVersionUID = 1L;
    TeacherServiceImpl teacherService;
    @RequestMapping("/ShowTeacher")
    protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        JSONArray arr = new JSONArray();
        ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
        teacherService=(TeacherServiceImpl) applicationContext.getBean("TeacherServiceImpl");
        ArrayList<BeanTeacher> teachers;
        try {
            teachers = teacherService.getTeachers();
            for(BeanTeacher teacher:teachers) {
                arr.add(teacher.toJson());
            }
        } catch (BooksNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.getWriter().append(arr.toJSONString());
    }
}
